package by.bsu.touragency.command.guest;

import org.apache.log4j.Logger;

import by.bsu.touragency.db.beans.Person;
import by.bsu.touragency.db.dao.PersonDAO;

public class RegistrationLogic {

	private static Logger logger = Logger.getLogger("log");

	public static boolean checkRegistration(String login, String pass,
			String email, boolean isAdmin) {
		logger.info("Registration");
		Person c = new Person();
		if (c.validityCheckLogin(login) && c.validityCheckPassword(pass)
				&& c.validityCheckEmail(email)) {
			c.setLogin(login);
			c.setPassword(pass);
			c.setEmail(email);
			c.setAdmin(isAdmin);
			PersonDAO cd = new PersonDAO();
			cd.create(c);
			logger.debug("Valid login,password & email");
			return true;
		}
		logger.warn("Invalid password or login or email");
		return false;
	}

	public static boolean isExistLogin(String login) {
		PersonDAO cd = new PersonDAO();
		if (cd.isExistByLogin(login)) {
			logger.debug("Such login already not exists");
			return true;
		} else {
			logger.debug("Such login already exists");
			return false;
		}
	}
}
